const { ccclass, property } = cc._decorator;

import o from "./RollingNoticeCtrl"
import i, { OutSideLevelGuide } from "./PeripheralSystemMgr"
import r from "./MatchGameData"
import n from "./PageMgr"
import l from "./AudioManager"
import s from "./PlayerDataSys"
import c from "./SystemDataSys"
import u from "./EventMgr"
import p from "./GameEventType"
import d from "./frameworkManager"
import h from "./ClientData"
import f from "./SdkHelper"
import g from "./EngineUtil"
import y from "./MathUtil"
import m from "./AbTestMgr"
import _ from "./OutsideGuideExperimentMgr";
import MathUtils from "./MathUtil";

var v = ["成都", "重庆", "贵州", "云南", "桂林", "三亚", "澳门", "广州", "香港", "中国台湾", "厦门", "杭州", "上海", "南京", "黄山", "南昌", "长沙", "武汉", "洛阳", "西安", "泰山", "天津", "秦皇岛", "沈阳", "长春", "哈尔滨", "呼和浩特", "西宁", "敦煌", "新疆", "西藏", "北京"];
var b = ["蜀郡", "巴渝", "黔中", "滇境", "岭南", "琼州", "濠镜澳", "南粤", "香江", "东夷", "鹭岛", "钱塘", "沪城", "金陵", "徽州", "九江郡", "洞庭郡", "荆楚", "中原", "长安", "齐鲁", "海津", "冀州", "奉天", "辽东郡", "黑水", "蒙古", "雍州", "陇原", "西域", "乌斯藏", "京城"], C = ["大熊猫基地", "千厮门长江大桥", "黄果树瀑布", "玉龙雪山", "十里画廊", "亚龙湾", "大三巴牌坊", "广州塔", "庙街夜市", "台北101", "双子塔", "西湖", "东方明珠塔", "南京长江大桥", "山顶迎客松", "滕王阁", "岳麓山爱晚亭", "武汉大学", "白马寺", "大雁塔", "“五岳独尊”石刻", "天津之眼", "阿那亚", "故宫", "南湖公园", "冰雪大世界", "呼和塔拉草原", "塔尔寺", "莫高窟", "喀纳斯湖", "布达拉宫", "长城"]
var T = "https://static.dayclover.com/wx_game/feie/bg_";
@ccclass
class PeripheralSystemDataMgr {
    INCREASE_CITY_ARR = [5, 9, 13, 21, 32];
    INCREASE_CITY_ARR2 = [5, 9, 13, 25, 32];



    public traval_location_info = null;
    public topTagNodeArr = [];
    public sroll_msg_list = null;
    public extract_info = [];
    public mapPointArr = [];
    public outsideStep = 0;
    public travelEvent = null;
    public travelData = null;
    public hotAirBallonFlag = 0;
    public travel_reward_info = null;
    public cash = 0;
    public gold = 0;
    public clock_data = null;
    public yes_clock_data = null;
    public break_through_succeed = true;
    public after_watch_video = false;
    public rollingNoticeNode = null;
    public reduceCash = 0;
    public gameShowTopNodeArr = [];
    public extract_count = 0;
    public remoteBgArr = [];
    public fd_status = false;
    public clock_days = 0;
    public fake_balance = 0;

    constructor() {
        this.traval_location_info = null;
        this.topTagNodeArr = [];
        this.sroll_msg_list = null;
        this.extract_info = [];
        this.mapPointArr = [];
        this.outsideStep = 0;
        this.travelEvent = null;
        this.travelData = null;
        this.hotAirBallonFlag = 0;
        this.travel_reward_info = null;
        this.cash = 0;
        this.gold = 0;
        this.clock_data = null;
        this.yes_clock_data = null;
        this.break_through_succeed = true;
        this.after_watch_video = false;
        this.rollingNoticeNode = null;
        this.reduceCash = 0;
        this.gameShowTopNodeArr = [];
        this.extract_count = 0;
        this.remoteBgArr = [];
        this.fd_status = false;
        this.clock_days = 0;
        this.fake_balance = 0;
    }
    static _instance: PeripheralSystemDataMgr;
    static _getInstance(): PeripheralSystemDataMgr {
        PeripheralSystemDataMgr._instance || (PeripheralSystemDataMgr._instance = new PeripheralSystemDataMgr());
        return PeripheralSystemDataMgr._instance;
    };
    public setPeripheralSystemInfo(e) {
        var t = e.clock_days, a = e.clock_data, o = e.yes_clock_data, i = e.sroll_msg_list, r = e.step, n = e.traval_data, l = e.travel_reward_info, s = e.event_type, c = e.extract_count;
        d.log("周边系统信息", e);
        this.setSrollMsgList(i);
        this.setTravelLocation(n);
        this.setOutsideStep(r);
        this.setTravelRewardInfo(l);
        this.setTravelEvent(s);
        this.setClockData(a);
        this.setClockDays(t);
        this.setYesClockData(o);
        this.setExtractCount(c);
        this.proloadBg();
    };
    public setTravelData(e) {
        d.log("设置旅行数据=====", e);
        var t = e.event_type, a = e.travel, o = e.red_flag, i = e.travel_reward_info, r = e.cash, n = e.gold, l = e.clock_info;
        this.travelEvent = t;
        this.travelData = a;
        this.setHotAirBallonFlag(o);
        // this.setTravelLocation(a.traval_location_info);
        this.setTravelRewardInfo(i);
        // this.setClockData(l.progress);
        // this.setClockDays(l.clock_days);
        // this.setRewardCash(r);
        // this.setRewardGold(n);
    };
    public dealProLoadBg() {
        this.deleteRemoteBg();
        this.proloadBg();
    };
    public getFakeBalance() {
        return this.fake_balance;
    };
    public setFakeBalance(e) {
        this.fake_balance = e || 0;
    };
    public setRewardCash(e) {
        this.cash = e;
    };
    public getRewardCash() {
        return this.cash;
    };
    public setRewardGold(e) {
        this.gold = e;
    };
    public getRewardGold() {
        return this.gold;
    };
    public setAfterWatchVideo(e) {
        this.after_watch_video = e;
    };
    public getAfterWatchVideo() {
        return this.after_watch_video;
    };
    public setHotAirBallonFlag(e) {
        this.hotAirBallonFlag = e;
    };
    public getHotAirBallonFlag() {
        return this.hotAirBallonFlag;
    };
    public getTravleData() {
        return this.travelData;
    };
    public setTravelEvent(e) {
        this.travelEvent = e;
    };
    public getTravelEvent() {
        return this.travelEvent;
    };
    public setTravelLocation(e) {
        this.traval_location_info = e;
    };
    public getTravelLocation() {
        return this.traval_location_info;
    };
    public setTravelRewardInfo(e) {
        this.travel_reward_info = e;
    };
    public getTravelRewardInfo() {
        return this.travel_reward_info;
    };
    public setSrollMsgList(e) {
        this.sroll_msg_list = this.dealScrollMsgList(e);
    };
    public dealScrollMsgList(e) {
        for (var t = [], a = 0; a < e.length; a++) {
            var o = e[a];
            if (1 == o.type) {
                for (var i = [], r = 0; r < 3; r++) i.push(o);
                t.push(i);
            } else this.isHitOppoReviewBan() ? 2 != o.type && t.push(o) : t.push(o);
        }
        t = g.shuffle(t);
        t = this.flatten(t);
        d.log("处理之后的数据滚动字幕=======", t);
        return t;
    };
    public flatten(e) {
        var t = this;
        return e.reduce(function (e, a) {
            return e.concat(Array.isArray(a) ? t.flatten(a) : a);
        }, []);
    };
    public getSrollMsgList() {
        return this.sroll_msg_list;
    };
    public setTopTagArr(e) {
        this.topTagNodeArr = e;
    };
    public getTopTagArr() {
        return this.topTagNodeArr;
    };
    public getTagNodeByType(e) {
        for (var t = 0; t < this.topTagNodeArr.length; t++) {
            var a = this.topTagNodeArr[t], o = a.type, i = a.tagNode;
            if (e == o) return i;
        }
        return null;
    };
    public setExtractInfo(e) {
        this.extract_info = e;
    };
    public getExtractInfo() {
        return this.extract_info;
    };
    public getDefaultSelectIndex() {
        for (var e = null, t = 0; t < this.extract_info.length; t++) 2 == this.extract_info[t].status && (e = t);
        null != e && (e = e >= 3 ? 3 : e);
        return e;
    };
    public getFinshCityIdByIndex(e) {
        for (var t = 0; t < this.extract_info.length; t++) {
            var a = this.extract_info[t].conditions[0].finish;
            if (e == t) return a;
        }
        return null;
    };
    public getCityNameByCityId(e) {
        var t = m.isDmthFlag(), a = this.isHitOppoReviewBan();
        return m.getDmth2Ab1() ? Number(e) + 1 == 32 ? "终点" : "第" + (e + 1) + "个城市" : m.getDmth2Ab2() ? C[e] : t ? a ? Number(e) + 1 == 32 ? "终点" : "第" + (e + 1) + "个城市" : b[e] : r.isOppoChannel() ? Number(e) + 1 == 32 ? "终点" : "第" + (e + 1) + "个城市" : v[e];
    };
    public setMapPointArr(e) {
        this.mapPointArr = e;
    };
    public getMapPointArr() {
        return this.mapPointArr;
    };
    public addLocalMapPointJson() {
        var e = this;
        g.addLocalJson("travelPath/aniPath2", function (t) {
            e.mapPointArr = t;
        });
    };
    public setOutsideStep(e) {
        this.outsideStep = e;
    };
    public getOutsideGuideStep() {
        return 15//this.outsideStep;
    };
    public isArriveNewCity() {
        return !(!this.travelData || !this.travelData.update_flag);
    };
    public getCurrentLocation() {
        return this.traval_location_info && this.traval_location_info.location ? this.traval_location_info.location : null;
    };
    public checkNextOutSideGuide() {
        this.outsideStep == OutSideLevelGuide.BREAKTHROUGH && this.showOutsideGuidePage();
    };
    public showOutsideGuidePage() {
        // m.isOptimizeTravelFundFlag() ? n.showPage("OutSideLevelGuideExperimentPage") : n.showPage("OutsideLevelGuidePage");//取消外面新手
    };
    public checkShowGameStartPage(e?) {
        void 0 === e && (e = false);
        if (m.isNoGqtcFlag()) this.checkShowEnterBtn(e); else {
            var t;
            t = m.isOptimizeTravelFundFlag() ? _.OutSideLevelGuideExperimentStep.CONTINUE_GAME : OutSideLevelGuide.CONTINUE_GAME;
            if (this.outsideStep >= t) {
                var a = this.checkShowUnluckPage();
                if (a.isShow && !e) {
                    n.showPage("MatchGameUnlockPropPage", a.data);
                    return;
                }
                if (this.checkGuideClock()) {
                    this.showOutsideGuidePage();
                    return;
                }
                u.trigger(p.SET_TOP_NODE_CANTOUCH, {
                    isCanTouch: true
                });
                n.hasShowPage("CashWithDrawPage") || n.hasShowPage("FundWithDrawPage") || n.hasShowPage("FundWithDrawBanPage") || n.hasShowPage("HotAirBallonPage") || n.hasShowPage("ClickEveryDayPage") || n.hasShowPage("SettingPage") ? u.trigger(p.SHOW_GAME_START_PAGE, {
                    isNeddPop: false
                }) : u.trigger(p.SHOW_GAME_START_PAGE, {
                    isNeddPop: true
                });
            }
        }
    };
    public checkShowEnterBtn(e) {
        void 0 === e && (e = false);
        var t;
        t = m.isOptimizeTravelFundFlag() ? _.OutSideLevelGuideExperimentStep.CONTINUE_GAME : OutSideLevelGuide.CONTINUE_GAME;
        if (this.outsideStep >= t) {
            var a = this.checkShowUnluckPage();
            if (a.isShow && !e) {
                n.showPage("MatchGameUnlockPropPage", a.data);
                return;
            }
            if (this.checkGuideClock()) {
                this.showOutsideGuidePage();
                return;
            }
            u.trigger(p.SET_TOP_NODE_CANTOUCH, {
                isCanTouch: true
            });
            u.trigger(p.ENTER_GAME_SHOW);
        }
    };
    public checkGuideClock() {
        var e = this.clock_data.get_status, t = this.clock_days;
        return 2 == e && 0 == t;
    };
    public checkShowUnluckPage() {
        var e = r.gameLevel, t = r.propInfo, a = {
            isShow: false,
            data: null
        };
        m.isNoGqtcFlag() && (e = r.gameLevel - 1);
        if (7 == e && t[0] && t[0].skillCount > 0) {
            a.isShow = true;
            a.data = {
                type: t[0].skillType,
                count: t[0].skillCount
            };
        }
        if (13 == e && t[1] && t[1].skillCount > 0) {
            a.isShow = true;
            a.data = {
                type: t[1].skillType,
                count: t[1].skillCount
            };
        }
        if (16 == e && t[2] && t[2].skillCount > 0) {
            a.isShow = true;
            a.data = {
                type: t[2].skillType,
                count: t[2].skillCount
            };
        }
        return a;
    };
    public setClockDays(e) {
        this.clock_days = e;
        u.trigger(p.UPDATE_COLCK_DAYS);
    };
    public getClockDays() {
        return this.clock_days;
    };
    public setClockData(e) {
        this.clock_data = e;
    };
    public getClockData() {
        return this.clock_data;
    };
    public setYesClockData(e) {
        this.yes_clock_data = e;
    };
    public getYesClockData() {
        return this.yes_clock_data;
    };
    public dealBreakthroughGuide(e) {
        var t, a = m.isOptimizeTravelFundFlag();
        t = a ? _.OutSideLevelGuideExperimentStep.CONTINUE_GAME : OutSideLevelGuide.CONTINUE_GAME;
        if (this.outsideStep == OutSideLevelGuide.FUND_WITHDRAW) if (0 == e.is_tg) {
            this.break_through_succeed = false;
            this.showOutsideGuidePage();
        } else {
            this.outsideStep = OutSideLevelGuide.BREAKTHROUGH;
            1 == r.gameLevel && this.showOutsideGuidePage();
        } else if (this.outsideStep != OutSideLevelGuide.MOVE_REWARD || a) this.outsideStep >= t && u.trigger(p.CLOSE_OUTSIDE_GUIDE); else {
            this.outsideStep = OutSideLevelGuide.CONTINUE_GAME;
            i.reportOutsideGuideStep(this.outsideStep);
            u.trigger(p.CLOSE_OUTSIDE_GUIDE);
        }
    };
    public setRollingNoticeNode(e) {
        r.isEditor || (this.rollingNoticeNode = e);
    };
    public getRollingNoticeNode() {
        return this.rollingNoticeNode;
    };
    public setRollingNoticeParent(e) {
        var t = this;
        if (!r.isEditor) if (e) if (this.rollingNoticeNode) if (this.rollingNoticeNode.parent) {
            this.rollingNoticeNode.removeFromParent();
            var a = this.rollingNoticeNode.getComponent(o);
            a || this.rollingNoticeNode.addComponent(o);
            var i = a.fNode, n = a.sNode, l = a.topY;
            if (i && n) {
                i.y = 0;
                n.y = -l;
            }
            setTimeout(() => {
                e.addChild(t.rollingNoticeNode);
                a.runNoticeAction(a.fNode, a.sNode);
                t.setRollingNoticeOpacity(255);
            }, 30);
        } else d.log("滚动公告节点父节点不存在"); else d.log("滚动公告节点不存在"); else d.log("滚动公告的父节点不存在请检查");
    };
    public setRollingNoticeOpacity(e) {
        r.isEditor || (this.rollingNoticeNode.opacity = e);
    };
    public setReduceCash(e) {
        this.reduceCash = e;
    };
    public getReduceCash() {
        return this.reduceCash;
    };
    public setGameShowTopNodeArr(e) {
        this.gameShowTopNodeArr = e;
    };
    public getGameShowTopNodeArr() {
        return this.gameShowTopNodeArr;
    };
    public getGameShowTopNodeByType(e) {
        for (var t = 0; t < this.gameShowTopNodeArr.length; t++) {
            var a = this.gameShowTopNodeArr[t], o = a.type, i = a.topNode;
            if (e == o) return i;
        }
        return null;
    };
    public isCanWithDraw() {
        if (this.extract_count >= 5) return false;
        var e = s.cash_balance, t = MathUtils.accDiv(e, 100);
        t = MathUtils.accMul(t, .01);
        var a = g.formatDecimal(t, 2), o = Number(a), i = r._gameLevel;
        m.isNoGqtcFlag() && (i = r._gameLevel - 1);
        return o >= .1 && i >= 2;
    };
    public setExtractCount(e) {
        this.extract_count = e;
    };
    public getExtractCount() {
        return this.extract_count;
    };
    public isShowClock() {
        var e = this.clock_data.get_status, t = this.getCurrentLocation();
        return 2 == e && t >= 11;
    };
    public reportVideoBuriedPoint(e) {
        f.reportData(e);
    };
    public reportEncourageVideo() {
        f.reportData("encourage_video");
    };
    public dealPlayBgMusic(e, t?) {
        void 0 === t && (t = false);
        if (l.getInstance().getMusicState()) if (e) {
            l.getInstance().pauseMusic("bgm_outSide", true);
            l.getInstance().playMusic("bgm", true, true);
        } else {
            l.getInstance().pauseMusic("bgm", true);
            t || l.getInstance().playMusic("bgm_outSide", true, true);
        } else {
            l.getInstance().pauseMusic("bgm_outSide", true);
            l.getInstance().pauseMusic("bgm", true);
        }
    };
    public proloadBg() {
        var e = this, t = this.getCurrentLocation();
        if (t) if (this.getCurryCityBg()) {
            var a = t + 1 >= v.length - 1 ? v.length - 1 : t + 1, o = T + a + ".png";
            this.getCurryCityBg(a) || g.loadRemoteImg(o).then(function (t) {
                d.log("预加载成功下下个=======", o);
                var i = {
                    location: a,
                    cityBg: t
                };
                e.remoteBgArr.push(i);
            }).catch(function () {
                d.log("预加载下下个背景失败=======", o);
            });
        } else for (var i = t + 1 >= v.length - 1 ? v.length - 1 : t + 1, r = function (t) {
            if (!n.getCurryCityBg(t)) {
                var a = T + t + ".png";
                g.loadRemoteImg(a).then(function (o) {
                    d.log("预加载成功下一个背景=======", a);
                    var i = {
                        location: t,
                        cityBg: o
                    };
                    e.remoteBgArr.push(i);
                }).catch(function () {
                    d.log("预加载背景失败=======", a);
                });
            }
        }, n = this, l = t; l <= i; l++) r(l); else d.log("预加载时获取的城市不对");
    };
    public getCurryCityBg(e?) {
        void 0 === e && (e = null);
        var t = this.getCurrentLocation();
        null != e && (t = e);
        if (!t) {
            d.log("获取当前背景当前城市获取错误");
            return null;
        }
        for (var a = 0; a < this.remoteBgArr.length; a++) {
            var o = this.remoteBgArr[a];
            if (o.location == t) return o.cityBg;
        }
        return null;
    };
    public deleteRemoteBg() {
        var e = this.getCurrentLocation();
        if (!e) {
            d.log("删除远程加载的背景当前城市获取错误");
            return null;
        }
        d.log("删除前远程加载的背景", this.remoteBgArr);
        for (var t = [], a = 0; a < this.remoteBgArr.length; a++) {
            var o = this.remoteBgArr[a];
            o.location >= e && t.push(o);
        }
        this.remoteBgArr = t;
        d.log("删除远程加载的背景", this.remoteBgArr);
    };
    public getRemoteUrlStr() {
        return T;
    };
    public checkShowPayMent() {
        2 == r.gameLevel && u.trigger(p.UPDATE_USER_CASH);
    };
    public isHitVivoReviewBan() {
        var e = s.is_reviewer, t = h.channel_name, a = c.isVivoer;
        return !(1 != e && !a || "vivo" != t);
    };
    public isHitVivoNewUser() {
        var e = c.new_add_flag, t = h.channel_name;
        return !(!e || "vivo" != t);
    };
    public isHitOppoReviewBan() {
        var e = s.is_reviewer, t = h.channel_name;
        return 1 == e && "oppo" == t;
    };
    public setFdStatus(e) {
        this.fd_status = e;
    };
    public getFdStatus() {
        return this.fd_status;
    };
    public isRewardCashAndGold() {
        return this.gold > 0 && this.cash > 0;
    };
    public getInsertAdShowLevel() {
        var e = m.isForceVideoFlagS1(), t = m.isForceVideoFlagS2(), a = m.isInsertAdFlagS1(), o = 12;
        e || t ? o = 7 : a && (o = 19);
        return o;
    };
    public getToNextCityPercent() {
        var e = this.getTravelLocation(), t = (e.current, e.location, e.need), a = (e.need_total,
            e.now, e.split_list), o = e.total, i = e.xizang, n = r.gameLevel, l = 0;
        if (m.getIsLcsmzq() && n >= i) {
            for (var s = 0; s < a.length; s++) {
                var c = a[s], u = c.end, p = c.per, d = 0, h = 0;
                if (0 == s) {
                    d = u - i;
                    h = i;
                } else {
                    d = u - a[s - 1].end;
                    h = a[s - 1].end;
                }
                if (!(n > u)) {
                    l += (n - h) * p;
                    break;
                }
                l += d * p;
            }
            return 1 - l;
        }
        return MathUtils.accDiv(t, o);
    };

}

export default PeripheralSystemDataMgr._getInstance();
